package org.example.task.exception;

import org.example.task.constant.response.HttpStatus;
import org.example.task.model.vo.R;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R handleValidationException(MethodArgumentNotValidException exception) {
        return R.error(HttpStatus.BAD_REQUEST, exception.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }

    @ExceptionHandler(TokenAuthExpiredException.class)
    public R handleValidationToken(TokenAuthExpiredException ex) {
        return R.error(ex.getCode(), ex.getMessage());
    }

    @ExceptionHandler({AuthenticationException.class})
    public R handleAuthenticationException(AuthenticationException ex) {
        return R.error(HttpStatus.UNAUTHORIZED, ex.getMessage());
    }

    @ExceptionHandler(SystemException.class)
    public R doSystemException(SystemException ex) {
        //记录日志
        //发邮件给开发人员
        //发短信给运维
        return R.error(ex.getCode(), ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public R doException(Exception ex) {
        //记录日志
        //发邮件给开发人员
        //发短信给运维
        return R.error(ex.getMessage());
    }
}
